package com.zhupanlin.zhyl.user.api.controller;

import com.zhupanlin.zhyl.common.result.Result;
import com.zhupanlin.zhyl.common.utils.AuthContextHolder;
import com.zhupanlin.zhyl.model.user.UserInfo;
import com.zhupanlin.zhyl.model.vo.user.LoginVo;
import com.zhupanlin.zhyl.model.vo.user.UserAuthVo;
import com.zhupanlin.zhyl.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/12/1 12:34
 */
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    // 用户手机号登录接口
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
        Map<String, Object> map = userInfoService.loginUser(loginVo);
        return Result.ok(map);
    }

    // 用户认证接口
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo,
                           HttpServletRequest request) {
        userInfoService.userAuth(userAuthVo, request);
        return Result.ok();
    }

    // 获取用户 id 信息接口
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }
}
